using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using XNALibrary.Game;
using XNALibrary.Materials;
using XNALibrary.Entities;

namespace XNALibrary.Lighting
{
    public class Light
    {
        private Model model;
        private Vector4 position;
        private Vector3 lookAt;
        private Vector4 ambientColor;
        private Vector4 diffuseColor;
        private Vector4 specularColor;
        private ShadowMap shadowMap;
        private float lightPower;
        private float maxDepth;
        public RenderTarget2D depthRenderTarget;

        #region Properties
        public float LightPower
        {
            get { return lightPower; }
            set { lightPower = value; }
        }

        public float MaxDeph
        {
            get { return maxDepth; }
            set { maxDepth = value; }
        }

        public Matrix LightViewProject
        {
            get { return LightView * LightProject; }
        }

        public Matrix LightView
        {
            get { return Matrix.CreateLookAt(new Vector3(position.X, position.Y, position.Z), new Vector3(lookAt.X, lookAt.Y, lookAt.Z), new Vector3(0,1,0)); }
        }

        public Matrix LightProject
        {
            get { return Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver2, (float)BaseGame.device.Viewport.Width / (float)BaseGame.device.Viewport.Height, 1f, 10000f); }
        }

        public Vector3 LookAt
        {
            get { return lookAt; }
            set { lookAt = value; }
        }

        public Vector4 SpecularColor
        {
            get { return specularColor; }
            set { specularColor = value; }
        }

        public Vector4 DiffuseColor
        {
            get { return diffuseColor; }
            set { diffuseColor = value; }
        }

        public Vector4 AmbientColor
        {
            get { return ambientColor; }
            set { ambientColor = value; }
        }

        public Vector4 Position
        {
            get { return position; }
            set { position = value; }
        } 
        #endregion

        public Light(Vector4 position)
        {
            this.position = position;
            this.lookAt = Vector3.Zero;
            this.ambientColor = new Vector4(0.1843f, 0.3098f, 0.3098f, 1);
            this.diffuseColor = new Vector4(0.3921f, 0.5843f, 0.9294f, 1);
            this.specularColor = new Vector4(1, 1, 1, 1);
            this.maxDepth = 500f;
            this.lightPower = 2f;

            shadowMap = new ShadowMap();

            PresentationParameters pp = BaseGame.device.PresentationParameters;
            depthRenderTarget = new RenderTarget2D(BaseGame.device,
                pp.BackBufferWidth, pp.BackBufferHeight, 1,
                pp.BackBufferFormat, pp.MultiSampleType, pp.MultiSampleQuality);
        }

        public void Initialize()
        {
            model = BaseGame.content.Load<Model>("Resources/Models/Light");
        }

        public void GetShadowMap()
        {
            BaseGame.device.SetRenderTarget(0, depthRenderTarget);
            BaseGame.device.Clear(Color.White);
            foreach (Entity e in BaseGame.scene)
                e.Draw(shadowMap);

            BaseGame.device.ResolveRenderTarget(0);
            BaseGame.device.SetRenderTarget(0, null);
        }

        public void Draw()
        {
            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.World = Matrix.CreateTranslation(position.X, position.Y, position.Z);

                    effect.View = BaseGame.camera.View;
                    effect.Projection = BaseGame.camera.Projection;
                }
                mesh.Draw();
            } 
        }
    }
}
